package nz.ac.massey.jbuif.editor.components;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;

import nz.ac.massey.jbuif.editor.BeanEditor;


/**
 * Class DoubleEditorPanel
 * @author AMIR H KIOUMARS
 *
 */
public class DoubleEditorPanel extends BaseComponent implements KeyListener {

	private static final long serialVersionUID = 1L;
	private JTextField textField;

	
	/**
	 * DoubleEditorPanel
	 * @param p bean editor instance
	 * @param name string name
	 * @param displayName string name to be display
	 * @param pev an object
	 */
	public DoubleEditorPanel(BeanEditor p,String name,String displayName,Object pev) {
		super(p, name,displayName, pev);
		
		textField = new JTextField();
		
		if(pev!=null){
			textField.setText(pev.toString());
		}
		
		textField.addKeyListener(this);	

		super.addPropertyComponent(textField);	
	}	
			
	
	/**
	 * getNumber
	 * @return a double represents a number
	 */
	private Double getNumber(){
		
		if(super.getBeanEditor().validateProperty("double",textField.getText() )){
			super.setEntryValid(true);
			return Double.parseDouble(textField.getText());
		} else {
			super.setEntryValid(false);
			return null;
		}
		
	}

	
	/**
	 * keyReleased controls the key released event
	 * @param e KeyEvent instance
	 */
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		ActionEvent e1 = new ActionEvent(e.getSource(), e.getID(), "textChanged");
		super.setPropertyValue(this.getNumber());
		e1.setSource(this);
		getBeanEditor().actionPerformed(e1);
	}

	
	/**
	 * keyPressed
	 * @param arg0 KeyEvent instance
	 */
	@Override
	public void keyPressed(KeyEvent arg0) {}
	
	
	/**
	 * keyTyped - override - dose nothing
	 * @param arg0 KeyEvent instance
	 */
	@Override
	public void keyTyped(KeyEvent arg0) {}
}

